package com.lfc.myframe.utils

import android.content.Context
import android.widget.TextView
import com.lfc.myframe.R
import com.lfc.myframe.share.Const
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

/**
 * Created by LFC
on 2020/5/15.
 */
object YZMUtils {
    /**
     * 开始计时
     */
    fun startCountDown(
        tvGetCode: TextView,
        baseContext: Context
    ) {
        startCountDown(tvGetCode, baseContext, 1)
    }

    /**
     * @param tvGetCode
     * @param baseContext
     * @param type        1 带蓝色框  2 直接蓝色字体 3 灰色底白字
     */
    fun startCountDown(
        tvGetCode: TextView,
        baseContext: Context,
        type: Int
    ) {
        val codeTimes: Long = Const.YZM_TIME
        Observable.interval(0, 1, TimeUnit.SECONDS)
            .take(codeTimes - 1)
            .map { aLong -> codeTimes - aLong }
            .observeOn(AndroidSchedulers.mainThread())
            .doOnSubscribe {
                tvGetCode.isEnabled = false
                tvGetCode.isClickable = false
                //
                if (type == 1) tvGetCode.background =
                    baseContext.resources.getDrawable(R.drawable.bg_yzm_lock) else if (type == 2) tvGetCode.setTextColor(
                    -0x666667
                ) else if (type == 3) tvGetCode.background = baseContext.resources.getDrawable(
                    R.drawable.bg_yzm_lock
                )
            }
            .subscribe(object : Observer<Long?> {
                override fun onSubscribe(d: Disposable) {}
                fun onNext(value: Long?) {
                    if (type == 2) tvGetCode.text = baseContext.resources.getString(
                        R.string.YZM_Countdown2,
                        value
                    ) else tvGetCode.text = baseContext.resources.getString(
                        R.string.YZM_Countdown,
                        value
                    )
                }

                override fun onError(e: Throwable) {}
                override fun onComplete() {
                    tvGetCode.isEnabled = true
                    tvGetCode.isClickable = true
                    tvGetCode.text = baseContext.resources.getString(R.string.YZM_get)
                    if (type == 1) tvGetCode.background =
                        baseContext.resources.getDrawable(R.drawable.bg_yzm) else if (type == 2) tvGetCode.setTextColor(
                        -0xf83ea0
                    ) else if (type == 3) tvGetCode.background = baseContext.resources.getDrawable(
                        R.drawable.bg_yzm
                    )
                }

                override fun onNext(value: Long) {
                    if (type == 2) tvGetCode.text = baseContext.resources.getString(
                        R.string.YZM_Countdown2,
                        value
                    ) else tvGetCode.text = baseContext.resources.getString(
                        R.string.YZM_Countdown,
                        value
                    )
                }
            })
    }


}